// pages/home/home.js
const app = getApp();
Page({

      /**
       * 页面的初始数据
       */
      data: {
            avatarUrl: '',
            userInfo: {

            },
            sign: '已签到',
            finish: '0%',// 信息完成度
            exp: 0,
            text:'',
            fontSize:'28rpx'
      },
      editInfo(){
            wx.navigateTo({
                  url: '/pages/userInfo/userInfo',
            })
      },
      // 点击关于我们
      about() {
            wx.navigateTo({
                  url: '/pages/about/about'
            })
      },
      // 签到
      sign() {
            if (this.data.sign != '已签到') {
                  wx.showLoading({
                        title: '签到',
                  })
                  let userId = wx.getStorageSync('userId');
                  wx.request({
                        url: getApp().globalData.host + 'client/user/addExp?exp=20&userId=' + userId,
                        success: res => {
                              wx.setStorageSync('exp', res.data.data.exp);
                              this.setData({
                                    sign: '已签到',
                                    exp: res.data.data.exp
                              })
                              wx.showToast({
                                    title: '签到成功',
                              })
                        },
                        fail: () => {
                              wx.showToast({
                                    title: '签到异常',
                                    icon:'error'
                              })
                        },
                        complete: () => {
                              wx.hideLoading()
                        }
                  })
            }
      },
      toLogin() {
            wx.navigateTo({
                  url: '/pages/login/login',
            })
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
            // let userId=wx.getStorageSync('userId');
            // if(userId!=undefined && userId!=null && userId!=''){
            //   wx.request({
            //     url: app.globalData.host+'client/user/getSignStatus?userId='+userId,
            //     success:res=>{
            //       console.log(res);
            //     }
            //   })
            // }
      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
            if (typeof this.getTabBar === 'function' && this.getTabBar()) {
                  this.getTabBar().setData({
                        selected: 1
                  })
            }
            wx.showLoading({
                  title: '正在加载',
            })
            let userId = wx.getStorageSync('userId');
            if (userId != undefined && userId != null && userId != '') {
                  wx.request({
                        url: getApp().globalData.host+'client/user/userInfo?userId='+userId,
                        success:res=>{
                              this.setData({
                                    userInfo:res.data.data.userInfo,
                                    exp:res.data.data.exp,
                                    avatarUrl:wx.getStorageSync('avatarUrl') || '/icon/logo.png',
                                    sign:res.data.data.isSign?'已签到':'签到'
                              })
                              this.finish();
                              wx.setStorageSync('userInfo', res.data.data.userInfo);
                              wx.setStorageSync('exp', res.data.data.exp);
                        },
                        fail:()=>{
                              wx.showToast({
                                    title: '服务器异常',
                                    icon:'error'
                              });
                              this.setData({
                                    userInfo:wx.getStorageSync('userInfo'),
                                    exp:wx.getStorageSync('exp'),
                                    avatarUrl:wx.getStorageSync('avatarUrl') || '/icon/logo.png',
                              })
                              this.finish();
                        },
                        complete:()=>{
                              wx.hideLoading();
                        }
                  })
            }else{
                  this.setData({
                        avatarUrl: '',
                        userInfo: {
            
                        },
                        sign: '已签到',
                        finish: '0%',
                        exp: 0,
                        text:'',
                        fontSize:'28rpx'
                  })
                  wx.hideLoading();
                  wx.showModal({
                        title: '未登录',
                        content: '是否现在登录？',
                        success :res=> {
                              if (res.confirm) {
                                    wx.navigateTo({
                                          url: '/pages/login/login',
                                    })
                              } else if (res.cancel) {
                                    this.setData({
                                          avatarUrl:'/icon/logo.png',
                                    })
                                    wx.hideLoading();
                              }
                        }
                  })
            }

      },

      /**信息完成度 */
      finish() {
            let finish = 0;
            let userInfo = wx.getStorageSync('userInfo');
            let arr = Object.keys(userInfo);
            arr.forEach(item => {
                  if (userInfo[item] != 'null' && userInfo[item] != '' && userInfo[item] != null) {
                        finish++;
                  }
            })
            let f = ((finish / arr.length) * 100 + '').substring(0, 5);
            this.setData({
                  finish: f + '%'
            })
            
            this.init();
      },

      init(){
            let oldSetting=wx.getStorageSync('oldSetting');
            if(oldSetting){
                  this.setData({
                        fontSize:'40rpx'
                  })
            }else{
                  this.setData({
                        fontSize:'28rpx'
                  })
            }
      }

})